package com.example.designmodel;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ResponsebilityActivity extends AppCompatActivity {

    public static void launch(Context context) {
        Intent intent = new Intent(context, ResponsebilityActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_responsebility);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        responsibiity();
    }

    private void responsibiity(){
//        IWoman woman = new ConcreateWoman(IHandler.FATHER_LEVEL,"女儿的请求");
        Random randow = new Random();
        List<IWoman> womans = new ArrayList<>();
        IWoman woman = null;
        for (int i = 0 ; i < 5 ; i++){
            woman = new ConcreateWoman(randow.nextInt(3) + 1,"我要去逛街");
            womans.add(woman);
        }

        IHandler father= new Father();
        IHandler hasband = new Hasband();
        IHandler son = new Son();
        father.setNet(hasband);
        hasband.setNet(son);
        for (IWoman woman1 : womans){
            father.handlerMessage(woman1);
        }


    }

    public static interface IWoman{
        int getType();
        String request();
    }

    public static class ConcreateWoman implements IWoman{

        private int type;
        private String reqest;

        public ConcreateWoman(int type, String reqest) {
            this.type = type;
            this.reqest = reqest;
        }

        @Override
        public int getType() {
            return type;
        }

        @Override
        public String request() {
            return reqest;
        }
    }

    public static abstract class IHandler{
        public static final int FATHER_LEVEL = 1;
        public static final int HASBANT_LEVEL = 2;
        public static final int SON_LEVEL = 3;
        private int level;
        private IHandler nextHandler;

        public IHandler(int level) {
            this.level = level;
        }

        public void handlerMessage(IWoman woman){
            if(woman.getType() == level){
                String response = this.response(woman);
                Log.i("test","response = "+response);
            }else{
                if(nextHandler != null){
                    nextHandler.handlerMessage(woman);
                }else{
                    Log.i("test"," is last response");
                }
            }
        }

        abstract String response(IWoman woman);

        public void setNet(IHandler nextHandler){
            this.nextHandler = nextHandler;
        }

    }

    public static class Father extends IHandler{
        public Father() {
            super(IHandler.FATHER_LEVEL);
        }

        @Override
        String response(IWoman woman) {
            return "女儿向父亲请求 " + woman.request()+ "同意 ";
        }
    }

    public static class Hasband extends IHandler{
        public Hasband() {
            super(IHandler.HASBANT_LEVEL);
        }

        @Override
        String response(IWoman woman) {
            return "妻子向丈夫请求 " + woman.request()+ "同意 ";
        }
    }

    public static class Son extends IHandler{
        public Son() {
            super(IHandler.SON_LEVEL);
        }

        @Override
        String response(IWoman woman) {
            return "母亲向儿子请求 " + woman.request()+ "同意 ";
        }
    }


}
